package com.example.note.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.note.bean.Notepad;
import com.example.note.databinding.ItemListBinding;
import com.example.note.db.NoteDbHelper;
import com.example.note.ui.EditAddActivity;
import com.example.note.utils.MainActivity;

import java.util.ArrayList;
import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>
{
    List<Notepad> list = new ArrayList<>();
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        ItemListBinding binding = ItemListBinding.inflate(layoutInflater);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Notepad np = list.get(position);
        holder.binding.tvTitle.setText(np.getContent());
        holder.binding.tvTime.setText(np.getTime());
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showdeletepage(np,v.getContext());
                return false;
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity activity = (MainActivity) v.getContext();
                Intent intent = new Intent(activity, EditAddActivity.class);
                intent.putExtra("notepadid",np.getId());
                activity.startActivityForResult(intent,100);
            }
        });
    }

    private void showdeletepage(Notepad np, Context context) {
        AlertDialog alertDialog = new AlertDialog.Builder(context)
                .setTitle("删除")
                .setMessage("确定删除吗？")
                .setPositiveButton("确定", (dialog, which) -> {
                    NoteDbHelper db = new NoteDbHelper(context);
                    db.deletenotepad(np);
                    setlist(db.FindNotes());
                    Toast.makeText(context, "成功删除", Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                })
                .create();
        alertDialog.show();
    }

    public void setlist(List<Notepad> list){
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return list.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder
    {
        ItemListBinding binding;
        public ViewHolder(@NonNull ItemListBinding binding)
        {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
